package com.kingcat.smartchair.webview;

import android.app.Activity;
import android.app.AlertDialog;
import android.graphics.Bitmap;
import android.os.Build;
import android.util.Log;
import android.webkit.*;
import android.widget.Toast;

import com.kingcat.smartchair.utils.AppUtils;
import com.kingcat.smartchair.utils.DeviceUt;

import java.util.ArrayList;
import java.util.HashMap;

// 主要是对通知应用程序关于历史记录，重新提交请求，加载资源，网页开始加载，网页加载结束，以及接收到的各种错误，是否重载键盘事件，是否重载网页加载
public class SmartChairWebViewClient extends WebViewClient {

    private Activity mActivity;

    static String TAG = "SmartChairWebViewClient";

    static ArrayList<String> ref = new ArrayList<>();

    public void setActivity(Activity activity)
    {
        mActivity = activity;
    }

    // 页面载入完成
    @Override
    public void onPageFinished(WebView webView, String url) {
        super.onPageFinished(webView, url);

        Log.e(TAG, "当前页面：" + url);


        String title = webView.getTitle();
        Log.e(TAG, "当前页面title：" + title);

        // 设置当前URL
        AppUtils.setAppWebViewCurrUrl(url);

        // 设置当前解锁状态
        if(url.indexOf("unlockpage.aspx") > 0)
        {
            AppUtils.setAppIsUnlock(true);
        }
    }

    // 开始加载页面时回调
    @Override
    public void onPageStarted(WebView webView, String url, Bitmap favicon) {
        super.onPageStarted(webView, url, favicon);
    }

    // 网络不通或DNS服务器异常检测
    @Override
    public void onReceivedError(WebView webView, WebResourceRequest request, WebResourceError error) {
        super.onReceivedError(webView, request, error);

        Toast.makeText(mActivity, "连接异常：" + error.getErrorCode() + " 请求资源：" + request.getUrl(), Toast.LENGTH_LONG).show();


        // 请求整个页面出问题才转向
        if(request.getUrl().toString().toLowerCase().indexOf(".aspx") >= 0) {
            webView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    // 设备号
                    String udid = DeviceUt.GetUDID(AppUtils.getAppContext());

                    Log.e(TAG, "页面启动...");

                    webView.loadUrl("file:///android_asset/index.html?udid=" + udid);
                }
            }, 1000);
        }
    }

    @Override
    public void onReceivedHttpError(WebView webView, WebResourceRequest request, WebResourceResponse errorResponse)
    {
        //if(request.getUrl().toString().indexOf(""))
        super.onReceivedHttpError(webView, request, errorResponse);


        Log.e(TAG, "--------------页面错误 错误地址是：" + request.getUrl().toString() + " err code:" + errorResponse.getStatusCode());

        // 整个页面出问题才转向
        if(request.getUrl().toString().toLowerCase().indexOf(".aspx") >= 0)
        {
            webView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    // 设备号
                    String udid = DeviceUt.GetUDID(AppUtils.getAppContext());

                    Log.e(TAG, "页面启动...");

                    webView.loadUrl("file:///android_asset/index.html?udid=" + udid);
                }
            }, 1000);
        }
    }

    // 打开网页时不调用系统浏览器， 而是在本WebView中显示
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            view.loadUrl(request.getUrl().toString());
        } else {
            view.loadUrl(request.toString());
        }
        return true;
    }
}
